package name.vaccari.matteo.tai.lezione03;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TrisController {

	private String pathInfo;

	public List<String> execute(String pathInfo, String nextPlayer) {
		this.pathInfo = pathInfo;
		List<String> result = new ArrayList<String>();
		for (int i=1; i<pathInfo.length(); i++) {
			String value = pathInfo.substring(i, i+1);
			if (value.equals(".")) {				
				result.add(computeDotValue(i, nextPlayer));
			} else {
				result.add(value);
			}
		}
		return result;
	}

	private String computeDotValue(int position, String nextPlayer) {
		String valueForDot = "&nbsp;";
		if (null != nextPlayer) {
			String newUrl = pathInfo.substring(1, position)
				+ nextPlayer 
				+ pathInfo.substring(position + 1)
				+ "?nextPlayer=" + flipPlayer(nextPlayer)
				;
			valueForDot ="<a href='" + newUrl+ "'>" + nextPlayer + "</a>";
		}
		return valueForDot;
	}

	private String flipPlayer(String player) {
		return player.equals("X") ? "0" : "X";
	}

}
